using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using System.Xml;
using limpalex.bbparser.Interfaces;

namespace limpalex.bbparser
{
    public sealed class ParserOptions
    {
        public ParserOptions()
        {
            AllowDoubleBraces = true;
            AllowDoubleQuotes = true;
            AutoCloseTags = true;
            DoNotThrowExceptions = true;
            IgnoreInvalidTags = false;
            IgnoreUnknownTags = false;
        }

        public bool AllowDoubleBraces { get; set; }
        public bool AllowDoubleQuotes { get; set; }
        public bool AutoCloseTags { get; set; }
        public bool IgnoreUnknownTags { get; set; }
        public bool IgnoreInvalidTags { get; set; }
        public bool DoNotThrowExceptions { get; set; }
        public ITextProcessor PreProcessor { get; set; }
        public ITextProcessor PostProcessor { get; set; }

        public static ParserOptions Create(XmlNode node)
        {
            var result = new ParserOptions();
            var type = typeof (ParserOptions);

            foreach (XmlNode child in node.ChildNodes)
            {
                if (child.NodeType != XmlNodeType.Element)
                    continue;
                var name = child.Name;
                var value = child.InnerText;

                var property = type.GetProperty(name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
                if ( property == null )
                    continue;
                if (!property.CanWrite)
                    continue;
                if ( property.PropertyType == typeof(bool))
                {
                    property.SetValue(result, Boolean.Parse(value), null);
                }
            }
            return result;
        }
    }
}
